﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using UIWindows.GlobalStatics;
using UIWindows.Services.Context;
using UIWindows.Tools.Helper;
using XingLucifer.Devices;
using XingLucifer.IBase;
using XingLucifer.IBase.Enums;

namespace UIWindows.Services.Handle
{
    [Attributes.TaskClassAttribute(TaskTypes.后称重清零)]
    public class AfterWeighingZeroing : ServierBase
    {
        public AfterWeighingZeroing(DeviceConfigStatic deviceConfig, SystemConfigStatic systemConfig,
            UserConfigStatic userConfig, ParameterStatic parameterStatic, TemporaryStatic temporaryStatic, HomeStatic homeStatic, MesParameterStatic mesParameterStatic)
            : base(deviceConfig, systemConfig, userConfig, parameterStatic, temporaryStatic, homeStatic, mesParameterStatic)
        {

        }

        public override Task Handle(Context.DataContext context)
        {
            if (!ClearSignal(context)) { return Task.CompletedTask; }
            try
            {
                var plc = _deviceConfig.DeviceList.FirstOrDefault(x => x.ServiceName == context.ServiceName && x.TaskType == TaskTypes.扫描信号).Communication as OmronCIP;
                var devices = _deviceConfig.DeviceList.Where(x => x.ServiceName == context.ServiceName 
                && context.TaskType.ToString().Contains(x.TaskType.ToString()) 
                && x.DeviceType == context.DeviceType);
                if (devices.Count() > 0)
                {
                    bool[] alarm = new bool[devices.Count()];
                    for (int i = 0; i < 3; i++)
                    {
                        Parallel.ForEach(devices, device =>
                        {
                            if (device.Index == 999) { return; }
                            device.Communication.WriteSingle(0, 0, 0, 0);
                        });
                        Thread.Sleep(2000);
                        Parallel.ForEach(devices, device =>
                        {
                            if (device.Index == 999) { return; }
                            double? value = device.Communication.ReadSingle<double>(0, 0);
                            alarm[device.Index - 1] = value == null ? true : !(value >= -0.1 && value <= 0.1);
                        });
                        if (!alarm.Any(x => x))
                        {
                            break;
                        }
                    }
                    if (alarm.Any(x => x))
                    {
                        StringBuilder stringBuilder = new StringBuilder();
                        for (int i = 0; i < alarm.Count(); i++)
                        {
                            if (alarm[i])
                            {
                                stringBuilder.Append($"{(i + 1)}号,");
                            }
                        }
                        $"{context.TaskType} {stringBuilder} 无法清零！！！".MessageDialogs(MessageLevelType.警告, false, Enums.PopUpTypes.Global);
                        WriteCMD(1, plc, _systemConfig.IAddress.Alarm_Zeroing_AfterWeighing);
                    }
                }
            }
            catch (Exception ex)
            {
                RunLog($"清零异常：{ex}");
            }
            return base.Handle(context);
        }
    }
}
